﻿using System;

namespace BorgExt
{
    public class ExtensionType : Qualifiable
    {
        internal MetaElementCollection<Extension> extensions;
        internal Extension sharedExtension;

        internal CompositionEventHandler<ExtensionEventArgs>[] CreatedEventHandlers { get; set; }
        internal CompositionEventHandler<ExtensionEventArgs>[] DestroyedEventHandlers { get; set; }

        public Plugin Plugin { get; internal set; }

        public MetaElementCollection<PlugType> PlugTypes { get; internal set; }
        public MetaElementCollection<SlotType> SlotTypes { get; internal set; }

        public Type Type { get; internal set; }
        public string TypeName { get; internal set; }

        internal EventHandlerInfo OnCreated { get; set; }
        internal EventHandlerInfo OnDestroyed { get; set; }

        internal bool HasExtensionConstructor { get; set; }

        public override bool IsQualified
        {
            get { return Qualifier.IsQualified(this); }
        }

        public event CompositionEventHandler<ExtensionEventArgs> Created
        {
            add
            {
                lock (LockObject)
                    CreatedEventHandlers = ArrayEx.Add(CreatedEventHandlers, value);
            }
            remove
            {
                lock (LockObject)
                    CreatedEventHandlers = ArrayEx.Remove(CreatedEventHandlers, value);
            }
        }

        public event CompositionEventHandler<ExtensionEventArgs> Destroyed
        {
            add
            {
                lock (LockObject)
                    DestroyedEventHandlers = ArrayEx.Add(DestroyedEventHandlers, value);
            }
            remove
            {
                lock (LockObject)
                    DestroyedEventHandlers = ArrayEx.Remove(DestroyedEventHandlers, value);
            }
        }

        internal ExtensionType(int id, string name)
            : base(id, name)
        {
            extensions = new MetaElementCollection<Extension>();
        }

        public override bool Qualify()
        {
            return Qualifier.InternalWeakQualify(this);
        }

        internal override AssemblyElement GetAssemblyElement()
        {
            return Plugin;
        }
    }
}

